<template>
  <div class="login-container">
    <div class="header">
      <div class="title">
        <strong>Briup</strong>学生综合管理平台
      </div>
    </div>
    <!-- 中间 -->
    <div class="center">
      <!-- 表单 -->
      <div class="form">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left" size="small">
          <div style="text-align:center;height:48px;line-height:48px;font-weight:bold">用户名密码登录</div>
          <div style="width:80%;margin:0 auto;height:1px;border-bottom:1px solid #ccc;margin-bottom:1em"></div>
          <el-form-item prop="username">
            <span class="svg-container"> <svg-icon icon-class="user" /> </span>
            <el-input v-model="loginForm.username" placeholder="Username" type="text" ref="username" />
          </el-form-item>
          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <span class="svg-container"> <svg-icon icon-class="password" /> </span>
              <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="Password" />
              <span class="show-pwd" @click="showPwd"> <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" /> </span>
            </el-form-item>
          </el-tooltip>
          <div>
            <el-button :loading="loading"  style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>

            <div style="text-align:right">
              <span>新童鞋？点击 <el-button type="text" @click="toRegister">注册</el-button></span>
            </div>
          </div>
        </el-form>
        <!-- 表单 -->
      </div>
    </div>
    <div class="footer">
      <div class="copyright">
        上海杰普软件科技有限公司 沪ICP备07000318号-1
      </div>
    </div>
  </div>
</template>

<script>
import {post} from '@/utils/request'
export default {
  name: 'Login',
  data() {
    return {
      visible:false,
      form:{},
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur'}],
        password: [{ required: true, trigger: 'blur'}]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    toRegister(){
      this.$router.push({
        path:'/register'
      });
    },
    // 提交注册
    submitHandler(){
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          let url = '/user/register'
          post(url,this.form).then(response => {
            this.$message({type:"success",message:response.message})
            this.visible = false;
          })
        } else {
          return false;
        }
      });
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['registerForm'].clearValidate();
    },
    // 去保存
    toRegisterHandler(){
      this.form = {};
      this.visible = true;
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>

/* reset element-ui css */
.login-container {
  position: absolute;
  width: 100%;
  top: 0;
  bottom: 0;
  background-color: #f4f4f4;
  overflow-x: hidden;
  overflow-y: auto;
  
  .header {
    height: 300px;
    background-color: #4fbc8d;
    position: relative;
    color: #ffffff;
    .title {
      text-align: center;
      position: absolute;
      bottom: 180px;
      width: 100%;
      font-size: 2em;
    }
  }
  .center{
    box-sizing: border-box;
    position: relative;
    top: -120px;
    background-color: #ffffff;
    width: 60%;
    margin: 0 auto;
    border-radius: 5px;
    padding: 2em;
    box-shadow: 0 0 10px #ccc;
  }
   @media (max-width: 768px) {
    .center {
      width:80%;
    }
  }
  @media (min-width: 1024px) {
    .center {
      width: 500px;
    }
  }
  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    line-height: 3em;
    text-align: center;
    font-size: 12px;
    font-weight: bold;
    background-color: #ffffff;
  }
  
}
</style>
